<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <!-- <el-form-item label="工厂用户名" class="myform-item">
          <el-input
            v-model="form.name"
            placeholder="请输入工厂用户名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item> -->
        <el-form-item label="状态" class="myform-item">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="item in listStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="" size="default" @click="onAddUser()"
            >添加用户</el-button
          >
          <!-- <el-button type="primary">导出</el-button>
          <el-button type="primary">上传文件</el-button> -->
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="工厂名称" align="center" prop="factory_name">
      </el-table-column>
      <el-table-column label="工厂用户名称" align="center" prop="name">
      </el-table-column>
      <el-table-column label="工厂小程序" align="center" prop="home_level_name">
      </el-table-column>
      <el-table-column label="工厂客户端" align="center" prop="pc_level_name">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <div :style="{ color: row.status == 1 ? 'red' : '#67C23A' }">
            {{ row.status == 0 ? "正常" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            style="color: red; margin-right: 10px"
            @click="onDisable(row.id,1)"
            v-if="row.status == 0"
            >禁用</el-button
          >
          <el-button
            type="text"
            style="margin-right: 10px"
            @click="onDisable(row.id,0)"
            v-if="row.status != 0"
            >启用</el-button
          >
          <el-button
            type="text"
            v-if="row.status == 0"
            size="default"
            @click="onEdit(row.id)"
            >编辑身份</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminCodeGetFactoryUserList"
    />
    <el-dialog
      title="添加工厂用户"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item label="工厂名称" class="myform-item">
          <el-input
            v-model="userForm.factory_name"
            placeholder="请输入工厂名称"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="工厂用户名" class="myform-item" prop="name">
          <el-input
            v-model="userForm.name"
            placeholder="请输入工厂用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" class="myform-item" prop="phone">
          <el-input
            type="number"
            v-model="userForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%; margin: 30px 0"
            @click="onAddUserPup('userForm')"
            :loading="loading"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminCodeGetFactoryUserList,
  postAdminCodeSetUserInfotovoid,
  postAdminCodeAddFactoryUser,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      userForm: {
        fid: "",
        name: "",
        phone: "",
        factory_name: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入工厂用户名", trigger: "blur" },
        ],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
      listStatus: [
        {
          value: "0",
          label: "正常",
        },
        {
          value: "1",
          label: "禁用",
        },
      ],
      form: {
        name: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  mounted() {
    this.form.name = this.$route.query.name;
    this.userForm.fid = this.$route.query.id;
    this.userForm.factory_name = this.$route.query.name;
    let form = this.form;
    this.postAdminCodeGetFactoryUserList(form);
  },
  methods: {
    // 弹出层 - 添加工厂用户
    onAddUser() {
      this.userForm.name = "";
      this.userForm.phone = "";
      this.dialogVisible = true;
    },
    // 添加工厂用户
    onAddUserPup(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          postAdminCodeAddFactoryUser(that.userForm).then((res) => {
            this.dialogVisible = false;
            this.loading = false;
            let form = this.form;
            this.postAdminCodeGetFactoryUserList(form);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async onDisable(id,type) {
      let data = {
        id,
      };
      this.$confirm(`此操作将${type == 1 ?'禁用':'启用'}该工厂用户, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await postAdminCodeSetUserInfotovoid(data);
            let form = this.form;
            this.postAdminCodeGetFactoryUserList(form);
          } catch (error) {}
        })
        .catch(() => {});
    },
    // 编辑
    onEdit(id) {
      this.$router.push({ name: "PlantDetail", query: { id } });
    },
    // 采购列表
    async postAdminCodeGetFactoryUserList(data) {
      this.loading = true;
      this.form = data;
      postAdminCodeGetFactoryUserList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminCodeGetFactoryUserList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
